package org.ltc.alliance.controller;

import org.ltc.alliance.constant.ClubTypeConst;
import org.ltc.alliance.constant.StatusConst;
import org.ltc.alliance.constant.WebConst;
import org.ltc.alliance.model.ClubDomain;
import org.ltc.alliance.service.club.ClubService;
import org.ltc.alliance.service.power.PowerService;
import org.ltc.alliance.utils.APIResponse;
import org.ltc.alliance.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/club")
public class ClubController extends BaseController{

    @Autowired
    private ClubService clubService;
    @Autowired
    private PowerService powerService;

    @RequestMapping("/create")
    @ResponseBody
    public APIResponse<?> createClub(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubName = map.get("clubName");
        String clubType = map.get("clubType");
        String summary = map.get("summary");
        String reason = map.get("reason");


        String uid = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(WebConst.USER_IN_COOKIE)){
                uid = cookie.getValue();
            }
        }
        ClubDomain club = new ClubDomain();
        club.setCname(clubName);
        club.setCtype(ClubTypeConst.valueOf(clubType));
        club.setUid(Integer.parseInt(uid));
        club.setCsummary(summary);
        club.setCreason(reason);

        try{
            Integer cid = clubService.saveClubInfo(club);
            powerService.InitReleaser(Integer.parseInt(uid), cid);
        }
        catch(Exception e){
            return APIResponse.fail(e.toString());
        }

        return APIResponse.success();
    }

    // 获取申请社团列表，申请社团为空会导致返回数据结构与接口定义不一致
    @RequestMapping("/apply-list")
    @ResponseBody
    public APIResponse<?> getApplyClubList(
            HttpServletRequest request,
            HttpServletResponse response) {
        try {
            List<ClubDomain> clubs = clubService.getClubByCstatus(StatusConst.Club.APPLYING);
            return APIResponse.success(clubs);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }

    }

    // 获取社团列表
    @RequestMapping("/normal-list")
    @ResponseBody
    public APIResponse<?> getNormalClubList(
            HttpServletRequest request,
            HttpServletResponse response) {
        try {
            List<ClubDomain> clubs = clubService.getNormalClubBriefInfo();
            return APIResponse.success(clubs);
        }
        catch (Exception e){
            return APIResponse.success(e.toString());
        }

    }

    @RequestMapping("/club-details")
    @ResponseBody
    public APIResponse<?> getClubDetails(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");

        try {
            ClubDomain club = clubService.getClubByCid(Integer.parseInt(clubId));
            return APIResponse.success(club);
        }
        catch (Exception e){
            return APIResponse.success(e.toString());
        }
    }

    // 对申请社团进行审核
    @RequestMapping("/review")
    @ResponseBody
    public APIResponse<?> reviewClub(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody String body) {
        Map<String, String> map = GsonUtils.getMapFromJson(body, HashMap.class);
        String clubId = map.get("clubId");
        String clubPass = map.get("clubPass");

        try {
            Boolean b = null;
            if (clubPass.equalsIgnoreCase("true") || clubPass.equalsIgnoreCase("false")){
                b = Boolean.valueOf(clubPass);
            }
            clubService.alterCstatusByCid(Integer.parseInt(clubId), b , StatusConst.Club.APPLYING);
        }
        catch (Exception e){
            return APIResponse.fail(e.toString());
        }
        return APIResponse.success();

    }

}
